<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
	
	protected $_requestObj;
	
	protected function _initSession(){
		Zend_Session::start();
		$ss = new Zend_Session_Namespace(APPLICATION_KEY);		
		if(empty($ss->type)){
			$ss->type = 'Guest';
		}
		
		$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Zendvn_System_Request_Http());
	}
	
	protected function _initDb(){
		$optionResources = $this->getOption('resources');
		$dbOption = @$optionResources['db'];
		$dbOption['adapter'] 			= 'PDO_MYSQL';
		$dbOption['params']['host'] 	= 'localhost';
		$dbOption['params']['username'] = 'root';
		$dbOption['params']['password'] = '';
		$dbOption['params']['dbname'] 	= 'cms2014';
		
		$adapter 	= $dbOption['adapter'];
		$config 	= $dbOption['params'];
		
		$db = Zend_Db::factory($adapter,$config);
		$db->setFetchMode(Zend_Db::FETCH_ASSOC);
		$db->query("SET NAMES 'utf8'");
		$db->query("SET CHARACTER SET 'utf8'");
		Zend_Registry::set('connectDb',$db);
		Zend_Db_Table::setDefaultAdapter($db);
		
		return $db;
		
	}
	protected function _initDbCache(){
		$frontendOptions = array(
	 		'automatic_serialization' => true
		);
	      
      	$backendOptions = array(
            'cache_dir' => APPLICATION_PATH . '/cache/',
            'file_name_prefix' => 'zendvn_cache_prefix',
     	 );
	      
      	$cache = Zend_Cache::factory('Core',
          	'File',
            $frontendOptions,
            $backendOptions
     	);
      	Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
   	}
	
	public function _initTimeZone(){
	}
	
	public function _initCookie(){
	 	$user_info = new Zend_Session_Namespace('user_login');
	 	$seconds  = 60 * 60 * 24 * 60;
	 	if(!empty($user_info->email)){
			if($user_info->remember == 1 && $user_info->access == true && $user_info->logout != 1){
				setcookie('user_email', $user_info->email, time() + $seconds);
				setcookie('user_login', true, time() + $seconds);
			}elseif($user_info->remember == 0 && $user_info->logout == 1){
				setcookie('user_email', '', time() - $seconds);
				setcookie('user_login', '', time() - $seconds);
			}
			$user_info->unsetAll();
	 	}
	 	if($user_info->logout != 1){
			if(!empty($_COOKIE['user_email']) && $_COOKIE['user_login'] == true){
				$info = new Zendvn_System_Info();
				$userInfo = $info->getMemberInfo();
				if(empty($userInfo)){
					$info->setInfo($_COOKIE, 'cookie');
				}
			}
	 	}else{
	 		setcookie('user_email', '', time() - $seconds);
			setcookie('user_login', '', time() - $seconds);
	 	}
	}
	

	protected function _initLoadPlugin(){
		$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Zendvn_System_Plugin());
	}
	
	protected function _initLoadRouter(){
// 		$config = new Zend_Config_Ini(CONFIG_PATH . '/zvn_router.ini','setup-router');
// 		$objRouter = new Zend_Controller_Router_Rewrite();
// 		$router = $objRouter->addConfig($config,'routers');
// 		$front = Zend_Controller_Front::getInstance();
// 		$front->setRouter($router);
	}
}